﻿//初始化浏览器信息
var Sys = {};
var ua = navigator.userAgent.toLowerCase();
var s;
(s = ua.match(/firefox\/([\d.]+)/)) ? Sys.firefox = s[1] :
    (s = ua.match(/chrome\/([\d.]+)/)) ? Sys.chrome = s[1] :
        (s = ua.match(/opera.([\d.]+)/)) ? Sys.opera = s[1] :
            (s = ua.match(/version\/([\d.]+).*safari/)) ? Sys.safari = s[1] : 0;

Sys.ie = isIE();

if (Sys.ie) {
    var obj_nfc = document.getElementById('obj_ie');
}
if (Sys.firefox || Sys.chrome) {
    var obj_nfc = document.getElementById('obj_firefox_chrome');
}
if (Sys.opera) document.write('Opera: ' + Sys.opera);
if (Sys.safari) document.write('Safari: ' + Sys.safari);
var msg = "";//document.getElementById('TxtArea');

function isIE() {
    if (window.ActiveXObject || "ActiveXObject" in window) {
        return 1;
    }
    else {
        return 0;
    }
}


//连接读卡器
function Connect() {
    var iRet;
    try {
        if (isComOpen == false)         //如果读卡器的串口还没有打开
        {
            var hdev = 1;
            iRet = obj_nfc.initialcom(100, 115200);   //连接读写器，100为USB口， 115200为波特率
            if (iRet != -1) {

                icdev = iRet;
                var ncard = 0;
                var data = "";

                hdev = obj_nfc.beep(icdev, 10);   //使读写器设备蜂鸣
                //iRet=obj.request(icdev,0);
                //ncard=obj.anticoll(icdev,0);
                //msg.value =msg.value+iRet+"\n"+ncard+"\n";
                //iRet=obj.select(icdev,ncard);
                //msg.value =msg.value+iRet+"\n";
                //ncard=obj.findcard(iRet,1);

                //hdev=obj.loadkey(iRet,0,gl_sector,"ffffffffffff");
                //hdev=obj.authentication(iRet,0,gl_sector);
                //if(hdev!=0)alert("error");
                //hdev=obj.read(iRet,0,gl_BinBlock);

                //var chstr="";
                var hex = "";
                //hex=obj.charToHex(chstr,4);
                //if(chstr!="0123")msg.value="ERROR\n";
                //msg.value =msg.value+hex+"\n";
                //chstr = obj.hexTochar(hex,2);

                msg = hex + "\n" + "连接读卡器成功！\n";
                //msg.value=msg.value+hdev+"\n";
                isComOpen = true;             //读卡器设为打开状态
                // alert(msg);
            }
            else {
                msg = "读卡器连接失败\n";
                isComOpen = false;           //读卡器设为未打开状态
            }
        }
    } catch (e) {
        alert(e.message);
    }
    return;
}


// 寻卡
function Findcard() {
    var strcard = "";
    strcard = obj_nfc.findcardStr(icdev, 1);      //1为读多张卡模式
    if (strcard != "") {
        //msg.value = msg.value + "寻卡成功\n";
        hasCard = true;
        //if(nRead<1)    //第一次读卡号
        //{
        msg = msg.value + "卡号为：" + strcard + "\n";
        //}
        return strcard;
    }
    else {
        msg = msg + "寻卡失败\n";
        hasCard = false;        //设置为当前无卡状态
        alert('该卡不匹配');
    }
    return "";
}
//读卡
function Read() {
    // 如果等于1读取物理卡
    if(is_physical == 1) {
        return Findcard();
    }

    var iRet;
    var data = " ";

    if (icdev == 0) {
        msg = msg + "ERROR";
        return;
    }
    var strcard = "";
    //hdev=obj.loadkey(icdev,0,gl_sector,"ffffffffffff");
    strcard = obj_nfc.findcardStr(icdev, 1);//多卡模式(1)寻卡

    iRet = obj_nfc.authentication(icdev, 0, gl_sector);    //认证密码      0为模式，2为第2扇区
//	if(iRet)
//	{
    //	msg.value = msg.value + "密码校验失败\n";
    //}
    //else
//	{
    //	msg.value = msg.value + "密码校验成功\n";
    //}

    //data =obj.read(icdev,gl_BinBlock);        //8为读第八块的数据
    data = obj_nfc.directRead(icdev, gl_BinBlock);        //直接读块中数据，可能不可见
    data2 = obj_nfc.directRead(icdev, gl_BinBlock_t);
    hdev = obj_nfc.beep(icdev, 10);   //使读写器设备蜂鸣
    if (data != "") {
        msg = msg + "读卡成功！数据为：\n" + data + data2 + "\n";
        return data + data2;
    }
    else {
        msg = "读卡失败\n";
        alert(msg + "请刷新重试");
    }
    return "";
}


//写卡
function Write() {
    var strcard = "";
    var data = " ";
    var iRet = 0;

    strcard = obj_nfc.findcardStr(icdev, 1);//多卡模式(1)寻卡
    if (strcard != "") {
        msg = "寻卡成功\n";
    }
    else {
        msg = "录卡失败\n" + iRet;
    }

    iRet = obj_nfc.authentication(icdev, 0, gl_sector);//验证第2扇区        0为模式，2为第2扇区
    if (iRet) {
        msg = "密码校验失败\n";
    }
    else {
        msg = "密码校验成功\n";
    }

    //iRet = obj.write(icdev,gl_BinBlock,"1234567");   //8为写第八块的数据
    iRet = obj_nfc.directWrite(icdev, gl_BinBlock, "fuhiajfiafjaif");//直接写数据，汉字为8个，纯字符串为16个
    if (iRet == 0) {
        msg = "写卡成功\n";
    }
    else {
        msg = "写卡失败\n";
    }

    return;

}


//断开连接
function Disconnect() {
    var iRet;
    var data;
    iRet = obj_nfc.exit(icdev);
    if (iRet) {
        msg = "断开连接失败\n";
    }
    else {
        msg = "成功断开连接\n";

        isComOpen = false; //设置串口为关闭状态
    }

}


/*var nRead = 0;     //同一张卡循环读的次数
 var hasCard = false;
 var isComOpen = false;
 var icdev = 0;
 var gl_sector = 0;
 var gl_BinBlock = 1;
 var gl_BinBlock_t = 2;
 var gl_valBlock = 9;*/
var nRead = 0;     //同一张卡循环读的次数
var hasCard = false;
var isComOpen = false;
var icdev = 0;

// 扇区，小标从0开始，这里都写在page_script.html
/*var gl_sector = 1;
 // 块，一个扇区有四个块，从小标从0开始
 var gl_BinBlock = 5;
 var gl_BinBlock_t = 6;*/
/**
 * 读卡规则：
 * gl_sector
 * gl_BinBlock = gl_sector*4+1
 * gl_BinBlock_t = gl_BinBlock+1
 * @type {number}
 */

var gl_valBlock = 9;

